package com.itheima.test;

import com.itheima.domain.SysNewUser;
import com.itheima.domain.SysUser;
import com.itheima.domain.User;
import com.itheima.mapper.NewUserMapper;
import com.itheima.mapper.SysRoleMapper;
import com.itheima.mapper.SysUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisTest5 {

    private SysRoleMapper rolemapper;
    private SysUserMapper usermapper;
    private NewUserMapper newusermapper;

    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        rolemapper = sqlSession.getMapper(SysRoleMapper.class);
        usermapper = sqlSession.getMapper(SysUserMapper.class);
        newusermapper = sqlSession.getMapper(NewUserMapper.class);
    }


    @Test
    public void testSave() {
        SysUser user = new SysUser();
        user.setUsername("李珍珍");
        user.setEmail("2343@qq.com");
        user.setPassword("235112");
        user.setPhonenum("15433459230");
        usermapper.save(user);
        System.out.println("ID======"+user.getId());
    }

    @Test
    public void testUpdae() {
        SysUser user = new SysUser();
        user.setId(5);
        user.setUsername("丽丽斯");
        user.setEmail("12343@qq.com");
        user.setPassword("23535112");
        user.setPhonenum("15433459230");
        usermapper.update(user);
    }

    @Test
    public void testDelete() {
        usermapper.delete(6);
    }

    @Test
    public void testQueryBpp() {
//        List<SysNewUser> list = newusermapper.findAll();
//        for (SysNewUser user : list) {
//            System.out.println(user);
//        }
//        SysNewUser user = newusermapper.findById(1);
//            System.out.println(user);


        SysUser user = usermapper.findByUsernameAndPassword("刘敏", "15221222976");
            System.out.println(user);
    }

    @Test
    public void testQuery() {
//        List<SysUser> list = usermapper.findAll();
//        for (SysUser user : list) {
//            System.out.println(user);
//        }
//        SysUser user = usermapper.findById(1);
//        System.out.println(user);
//
//
//        List<SysRole> roleList = rolemapper.findAllbymidTbId(1);
//        for (SysRole role : roleList) {
//            System.out.println(role);
//        }
        List<SysUser> list = usermapper.findAllincludeRole();

        for (SysUser user : list) {
            System.out.println(user);
        }


    }


}
